package com.example.weatherview.view.a1

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import kotlin.math.roundToInt

/**
 * @author Tom.wu
 * @Description:
 * @Package: com.example.weatherview.view.a1
 * @data  2024/4/12 11:32
 */
class SunView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    companion object{
        const val TAG = "SunView"
    }

    private val mRectF = RectF()

    private val mMargin = dp2px(context, 38f).toFloat()

    private val mRadiusLine = Paint().apply {
        strokeWidth =  dp2px(context, 2f).toFloat()
        color = Color.RED
        style = Paint.Style.STROKE

    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val w = MeasureSpec.getSize(widthMeasureSpec)
        val h = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(w, h)
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mRectF.apply {
            top = 0F
            bottom = width-(2*mMargin)
            left = mMargin
            right =width-mMargin
        }
        Log.d(TAG, "onSizeChanged: $mRectF  $mMargin")
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            drawRect(mRectF,mRadiusLine)
            drawArc(mRectF,0f,360f,false,mRadiusLine)
        }
    }


    private fun dp2px(context: Context, dp: Float): Int {
        val density = context.resources.displayMetrics.density
        return (dp * density).roundToInt()
    }


}